package com.qinhai.webcs.controller;

import com.qinhai.webcs.pojo.Orders;
import com.qinhai.webcs.pojo.User;
import com.qinhai.webcs.service.OrderService;
import com.qinhai.webcs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @ClassName BackUserController
 * @Deacription
 * @Author qinhai
 * @Date 2021/4/20 15:25
 * @Version 1.0
 **/

@Controller
@RequestMapping("/back")
public class BackUserController {
    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/userList")
    public ModelAndView getUserList() {
        ModelAndView modelAndView = new ModelAndView();
        List<User> list = userService.list();
        modelAndView.addObject("userList", list);
        modelAndView.setViewName("back/userList");
        return modelAndView;
    }

    @GetMapping("/userType")
    public ModelAndView getUserType() {
        ModelAndView modelAndView = new ModelAndView();
        List<User> list = userService.list();
        modelAndView.addObject("userList", list);
        modelAndView.setViewName("back/userType");
        return modelAndView;
    }

    @GetMapping("/updateType/{id}")
    public String setUpdateType(@PathVariable("id") Integer id) {
        User user = userService.getById(id);
        user.setType(true);
        userService.updateById(user);
        return "redirect:/back/userType";
    }

    @GetMapping("/deleteType/{id}")
    public String setDeleteType(@PathVariable("id") Integer id) {
        User user = userService.getById(id);
        user.setType(false);
        userService.updateById(user);
        return "redirect:/back/userType";
    }

    @GetMapping("/ordersList")
    public ModelAndView getOrdersList() {
        ModelAndView modelAndView = new ModelAndView();
        List<Orders> list = orderService.list();
        modelAndView.addObject("orderList", list);
        modelAndView.setViewName("back/ordersList");
        return modelAndView;
    }

    @GetMapping("/goods/{id}")
    public String setGoods(@PathVariable("id") Integer id) {

        Orders orders = orderService.getById(id);
        orders.setGoods("已发货");
        orderService.updateById(orders);
        return "redirect:/back/ordersList";
    }
}
